package com.wy.cron;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wy.bean.FeeBean;
import com.wy.dao.HouseholdsMapper;
import com.wy.model.sys.FeeRule;
import com.wy.model.sys.Households;
import com.wy.model.sys.NoticeEntity;
import com.wy.model.sys.NoticeRelation;
import com.wy.service.wy.FeeService;
import com.wy.service.wy.HouseholdService;
import com.wy.service.wy.NoticeRelationService;
import com.wy.service.wy.NoticeService;
import com.wy.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: tracy
 * @Date: 2018/9/3 18:29
 * @Description:
 */
@Component
public class ScheduleModule {

    private Logger log = LoggerFactory.getLogger(ScheduleModule.class);

    @Resource(name = "noticeServiceImpl")
    private NoticeService noticeService;
    @Resource(name = "noticeRelationServiceImpl")
    private NoticeRelationService noticeRelationService;
    @Resource(name = "householdServiceImpl")
    private HouseholdService householdService;
    @Resource(name = "feeServiceImpl")
    private FeeService feeService;

    /**
     * 第一次延迟1秒执行，当执行完后3秒再执行
     * 定时设置消息发布，将公告通知发送到指定客户
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 60 * 1000)
    public void publishNotice(){
        try {
            //查询状态为未发送的通知或公告
            List<NoticeEntity> list = noticeService.getNoticeByPublishTime(DateUtils.getStringDate());
            String toUser = "";
            for (NoticeEntity noticeEntity : list) {
                toUser = noticeEntity.getToUsers();
                JSONArray jsonArray = JSONArray.parseArray(toUser);
                if(jsonArray  == null || jsonArray.size() == 0 ) {
                    continue;
                }
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Integer key = jsonObject.getInteger("type");
                    if (key == 3) { //id 住户的id
                        try {
                            List<String> ids = JSON.parseArray(jsonObject.getString("ids"), String.class);
                            //根据ids获取
                            List<Households> householdsList = householdService.getHouseholdByIds(ids);
                            List<NoticeRelation> noticeRelationList = new ArrayList<NoticeRelation>();
                            for (Households household : householdsList) {
                                NoticeRelation noticeRelation = new NoticeRelation();
                                noticeRelation.setNoticeId(noticeEntity.getId());
                                //当住户没有管理用户id时不推送消息或通知
                                if(null == household.getUserId()) {
                                    continue;
                                }
                                noticeRelation.setRelationId(household.getUserId()); //用户id
                                noticeRelation.setReadStatus("0");
                                noticeRelation.setRelationType(3);
                                noticeRelationList.add(noticeRelation);
                            }
                            noticeRelationService.insertBatch(noticeRelationList);
                        }catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                noticeEntity.setStatus("1");
                noticeService.updateNotice(noticeEntity);
            }
        }catch(Exception ex) {
            ex.printStackTrace();
        }

    }

    /**
     * 默认每个月1号凌晨1点生成账单
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 60 * 1000)//(cron = "0 05 11 * * ?")
    public void generateFeeRecord(){
        try {
            String today = DateUtils.getStringDate(new Date(),DateUtils.timeformat4);
            List<FeeBean> feeRules = feeService.getFeeRuleByTime(today);
            for(FeeBean feeBean : feeRules) {
                feeService.generateFeeRecord(feeBean);
            }
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 第一次延迟1秒执行，当执行完后3秒再执行
     * 定时设置消息失效
     */
//    @Scheduled(initialDelay = 1000, fixedDelay = 3000)
    public void invalidNotice(){
        //查询状态为未发送的通知或公告



    }


}
